package com.icetech.eom.service.impl.tx;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.icetech.eom.dao.tx.ServerDao;
import com.icetech.eom.domain.tx.Server;
import com.icetech.eom.domain.tx.dto.ServerExcelDto;
import com.icetech.eom.service.tx.IServerService;
import com.xuxueli.poi.excel.ExcelExportUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangzw
 * @since 2018-11-12
 */
@Service
public class ServerServiceImpl extends ServiceImpl<ServerDao, Server> implements IServerService {
    @Autowired
    private ServerDao serverDao;
    @Override
    public PageInfo<Server> selectByPage(String searchKey, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<Server> servers = serverDao.selectByServerName(searchKey);
        return new PageInfo(servers);
    }

    @Override
    public Workbook exportExcel(String searchKey) {
        List<Server> servers = serverDao.selectByServerName(searchKey);
        List<ServerExcelDto> serverExcelDtos = Lists.newArrayList();
        servers.forEach(server -> {
            ServerExcelDto serverExcelDto = new ServerExcelDto();
            if (server.getIsEnabled()){
                serverExcelDto.setIsEnabledStr("可用");
            }else {
                serverExcelDto.setIsEnabledStr("不可用");
            }
            BeanUtils.copyProperties(server, serverExcelDto);
            serverExcelDtos.add(serverExcelDto);
        });

        return ExcelExportUtil.exportWorkbook(serverExcelDtos);
    }

}
